package org.firecloud.h5fun.controller.service;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.firecloud.h5fun.model.CommonConfig;
import org.firecloud.h5fun.model.CommonQuery;
import org.firecloud.h5fun.model.javabean.CommentaryBean;
import org.json.JSONArray;
import org.json.JSONObject;
/**
 * 获取Commentary信息通过Ajax方法调用URL：ajax_get_commentarys<br>
 * 必须登陆：false<br>
 * 必须管理：false<br>
 * 参数：<br>
 * appId，非必须，获取评论的Application的ID<br>
 * projectId，非必须，获取评论的Project的ID<br>
 * page，非必须，页码<br>
 * 注：projectId和appId必须有一个<br>
 * @author fc
 */
public class AjaxGetCommentarys extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Connection conn = null;
		JSONObject json = new JSONObject();
		try {
			resp.setCharacterEncoding("UTF-8");
			resp.setContentType("application/json");
			CommonConfig config = (CommonConfig) req.getServletContext().getAttribute("commonConfig");
			int appId = 0;
			int projectId = 0;
			int page = 0;
			String strAppId = req.getParameter("appId");
			if (strAppId != null) {
				appId = Integer.parseInt(strAppId);
			}
			String strProjectId = req.getParameter("projectId");
			if (strProjectId != null) {
				projectId = Integer.parseInt(strProjectId);
			}
			String strPage = req.getParameter("page");
			if (strPage != null) {
				page = Integer.parseInt(strPage);
			}
			if (projectId != 0 || appId != 0) {
				conn = config.getConn();
				List<CommentaryBean> commentarys = new ArrayList<CommentaryBean>();
				int size = CommonQuery.select_commentarys(conn, commentarys, projectId, appId, page, config.getCommentaryPageSize());
				json.put("type", 0);
				json.put("maxPage", (size-1)/config.getCommentaryPageSize());
				JSONArray datas = new JSONArray();
				for(CommentaryBean commentary:commentarys){
					JSONObject data = new JSONObject();
					data.put("userId", commentary.getUserId());
					data.put("userName", commentary.getUser().getName());
					data.put("date", simpleDateFormat.format(commentary.getCreateTime()));
					data.put("context", commentary.getContext());
					datas.put(data);
				}
				json.put("datas", datas);
			} else {
				json.put("type", -3);
				json.put("msg", "无评论对象");
			}

		} catch (SQLException e) {
			json.put("type", -1);
			json.put("msg", "查询错误");
			e.printStackTrace();
		} catch (Exception e) {
			json.put("type", -2);
			json.put("msg", "未知错误");
			e.printStackTrace();
			Logger.getRootLogger().error(e);
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (Exception e) {
					Logger.getRootLogger().error(e);
				}
			}
		}
		resp.getWriter().print(json);
	}
}
